package org.xb.dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import org.xb.vo.Admin;


public class AdminDAOImpl implements AdminDAO{
	private Connection conn;
	private PreparedStatement pstmt;
	public AdminDAOImpl(Connection conn){
		this.conn=conn;
	}
public Admin findLogin(String adname,String adpassword)throws Exception{
	Admin admin=null;
	String sql="select addid,adname,adpassword from admins where adname=? and adpassword=?";
	this.pstmt=this.conn.prepareStatement(sql);
	this.pstmt.setString(1,adname);
	this.pstmt.setString(2,adpassword);
	ResultSet rs=this.pstmt.executeQuery();
	while(rs.next()){
		admin=new Admin();
		admin.setAdid(rs.getInt(1));
		admin.setAdname(rs.getString(2));
		admin.setAdpassword(rs.getString(3));
	}
	return admin;
}
public boolean doCreate(Admin admin) throws Exception {
	boolean flag=false;
	String sql="insert into admins(adname,adpassword) values(?,?)";
	this.pstmt=this.conn.prepareStatement(sql);
	this.pstmt.setString(1,admin.getAdname());
	this.pstmt.setString(2,admin.getAdpassword());
	if(this.pstmt.executeUpdate()>0){
		flag=true;
	}
	return flag;
}
public long getCount(String keyword) throws Exception {
	long count=0;
	String sql="select count(*) from admnins where adname like ? or adpassowrd like ?";
	this.pstmt=this.conn.prepareStatement(sql);
	this.pstmt.setString(1,"%"+keyword+"%");
	this.pstmt.setString(2,"%"+keyword+"%");
	ResultSet rs=this.pstmt.executeQuery();
	while(rs.next()){
		count=rs.getLong(1);
	}
	return count;
}
public List<Admin> listAll(String keyword, int currentPage, int lineSize)
		throws Exception {
	List<Admin>all=new ArrayList<Admin>();
	String sql="select id,adname,adpassword form admins where adname like ? or adpassword like ? limit ?,?";
	this.pstmt=this.conn.prepareStatement(sql);
	this.pstmt.setString(1,"%"+keyword+"%");
	this.pstmt.setString(2,"%"+keyword+"%");
	this.pstmt.setInt(3,(currentPage-1)*lineSize);
	this.pstmt.setInt(4,lineSize);
	ResultSet rs=this.pstmt.executeQuery();
	while(rs.next()){
		Admin admin=new Admin();
		admin.getAdid();
		admin.getAdname();
		admin.getAdpassword();
		all.add(admin);
	}
	return all;
}
public Admin findById(Integer id) throws Exception {
	Admin admin=null;
	String sql="select adid,adname,adpassword where adid=?";
	this.pstmt=this.conn.prepareStatement(sql);
	this.pstmt.setInt(1,id);
	ResultSet rs=this.pstmt.executeQuery();
	while(rs.next()){
		admin=new Admin();
		admin.setAdid(rs.getInt(1));
		admin.setAdname(rs.getString(2));
		admin.setAdpassword(rs.getString(3));
	}
	return admin;
}
public boolean doDelete(Admin admin) throws Exception {
	boolean flag=false;
	String sql="delete from admins where adid=?";
	this.pstmt=this.conn.prepareStatement(sql);
	this.pstmt.setInt(1,admin.getAdid());
	if(this.pstmt.executeUpdate()>0){
		flag=true;
	}
	return flag;
}
public boolean doUpdate(Admin admin) throws Exception {
    boolean flag=false;
    String sql="update admins set adname=? and adpassword=? where adid=?";
    this.pstmt=this.conn.prepareStatement(sql);
    this.pstmt.setString(1,admin.getAdname());
    this.pstmt.setString(2,admin.getAdpassword());
    if(this.pstmt.executeUpdate()>0){
    	flag=true;
    }
	return flag;
}

}
